package com.yb.guigu.pick.pay.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.yb.guigu.pick.core.bean.AlipayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/30 17:54
 * FileName: AlipayNotifyController
 * Description:
 */

@RestController
@RequestMapping("/alipay/notify")
@Slf4j
public class AlipayNotifyController {

    @Autowired
    AlipayConfig alipayConfig;

    @RequestMapping("")
    public String handleNotify(@RequestParam Map<String,String> paramMap) throws AlipayApiException {

        log.info("{}",paramMap);

        boolean signVerified = AlipaySignature.rsaCheckV1(paramMap, alipayConfig.getAlipayPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType()); //调用SDK验证签名
        if(signVerified){
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商户自身业务处理，校验失败返回failure
            log.info("验签成功");
        }else{
            // TODO 验签失败则记录异常日志，并在response中返回failure.
            log.info("验签失败");
        }

        return "success";
    }
}
